﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.RouteSetting
{
    public partial class FrmRenameCamera : Form
    {
        public FrmRenameCamera()
        {
            InitializeComponent();
        }

        public RouteChannel Camera { get; set; }

        public void SetChannel(RouteChannel rc, dl_server_config server)
        {
            Camera = rc;

            if (Camera != null && server != null)
            {
                this.txtServer.Text = server.storage_server_name;
                this.txtChannelId.Text = rc.Channel.channel_id;
                this.txtChannelName.Text = rc.Channel.channel_name;
                this.txtChannelDesc.Text = rc.Channel.channel_desc;
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            string temp = Camera.Channel.channel_desc;

            Camera.CameraName = txtChannelDesc.Text.Trim();
            Camera.Channel.channel_desc = txtChannelDesc.Text.Trim();

            var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.UpdateMapChannel, JsonConvert.SerializeObject(Camera.Channel));

            if (result.success)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                Camera.CameraName = temp;
                Camera.Channel.channel_desc = temp;

                MessageBox.Show("修改摄像机信息失败!");                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
